/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scrumpmt.web;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.scrumpmt.domain.Colaborador;
import org.scrumpmt.ejb.ColaboradorFacade;
import org.scrumpmt.ejb.ProyectoFacade;

/**
 *
 * @author Cristian
 */
@ManagedBean
@RequestScoped
public class newUser extends BasicBean{
    private ColaboradorFacade colaboradorFacade = lookupColaboradorFacade();
    
    //private static final Logger log = Logger.getLogger(newUser.class.getName());
    
    private String nombre;
    private String password;
    private String password2;
    private String mail;
    private Boolean administrador;

    public Boolean getAdministrador() {
        return administrador;
    }

    public void setAdministrador(Boolean administrador) {
        this.administrador = administrador;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }
    
    public String volver (){
        return "index";
    }
    
    public String registrar (){
        if(datosValidos()){
            Colaborador c = new Colaborador();
        
            c.setAdministrador(administrador);
            c.setMail(mail);
            c.setNombre(nombre);
            c.setPassword(password);
            log.log(Level.WARNING, "Voy a insertar el nuevo usuario");
            colaboradorFacade.create(c);
            
                return "" + c.getId();
                        
        }else {
            log.log(Level.SEVERE,"Datos para crear usuario NO VALIDOS");
            return "";
        }
    }
     private boolean datosValidos(){
        if (mail != null 
                && nombre != null 
                && password != null
                && password != null
                ){
            return true;
        } else {
            return false;
        }
     }
    
    /**
     * Creates a new instance of newUser
     */
    public newUser() {
    }
    
        private ColaboradorFacade lookupColaboradorFacade() {
        try {
            Context c = new InitialContext();
            return (ColaboradorFacade) c.lookup("java:global/ScrumPMT/ScrumPMT-ejb/ColaboradorFacade!org.scrumpmt.ejb.ColaboradorFacade");
        } catch (NamingException ne) {
            log.log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
